package com.example.servlet;

import com.example.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        
        // 尝试查询用户，支持user和users表名
        String sql = "SELECT * FROM user WHERE username=? AND password=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, password);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    boolean isAdmin = rs.getBoolean("is_admin");
                    HttpSession session = req.getSession();
                    session.setAttribute("user_id", rs.getInt("id"));
                    session.setAttribute("username", username);
                    session.setAttribute("is_admin", isAdmin);
                    
                    // 创建User对象并设置到session中
                    com.example.model.User user = new com.example.model.User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(username);
                    user.setAdmin(isAdmin);
                    session.setAttribute("user", user);
                    
                    // 跳转到不同首页
                    if (isAdmin) {
                        session.setAttribute("loginSuccess", "管理员登录成功！欢迎回来，" + username);
                        resp.sendRedirect("admin-dashboard.jsp");
                    } else {
                        session.setAttribute("loginSuccess", "登录成功！欢迎回来，" + username);
                        resp.sendRedirect("index.jsp");
                    }
                    return;
                }
            }
        } catch (Exception e) {
            // 如果user表不存在，尝试users表
            try (Connection conn = DBUtil.getConnection();
                 PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE username=? AND password=?")) {
                ps.setString(1, username);
                ps.setString(2, password);
                try (ResultSet rs = ps.executeQuery()) {
                    if (rs.next()) {
                        boolean isAdmin = rs.getBoolean("is_admin");
                        HttpSession session = req.getSession();
                        session.setAttribute("user_id", rs.getInt("id"));
                        session.setAttribute("username", username);
                        session.setAttribute("is_admin", isAdmin);
                        
                        // 创建User对象并设置到session中
                        com.example.model.User user = new com.example.model.User();
                        user.setId(rs.getInt("id"));
                        user.setUsername(username);
                        user.setAdmin(isAdmin);
                        session.setAttribute("user", user);
                        
                        // 跳转到不同首页
                        if (isAdmin) {
                            session.setAttribute("loginSuccess", "管理员登录成功！欢迎回来，" + username);
                            resp.sendRedirect("admin-dashboard.jsp");
                        } else {
                            session.setAttribute("loginSuccess", "登录成功！欢迎回来，" + username);
                            resp.sendRedirect("index.jsp");
                        }
                        return;
                    }
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            e.printStackTrace();
        }
        req.setAttribute("error", "用户名或密码错误");
        req.getRequestDispatcher("login.jsp").forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("login.jsp").forward(req, resp);
    }
} 